/*
 * @Date: 2023-03-04 21:19:08
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-03-04 21:36:58
 * @FilePath: \A19\backend\src\api\SourceManage\ApiDeleteFolder.ts
 */
import { ApiCall } from "tsrpc";
import { ReqDeleteFolder, ResDeleteFolder } from "../../shared/protocols/SourceManage/PtlDeleteFolder";
import { Global } from '../../Global/index';
import { findFolder } from "../../utils/FindCurrentFolder";

export default async function (call: ApiCall<ReqDeleteFolder, ResDeleteFolder>) {
    // TODO
    const course = call.req.course
    const root = await Global.collection('folders').findOne({
        course
    })
    if(!root) return call.error('课程不存在')

    const folder = findFolder(call.req.path,root)
    if(!folder) return call.error('文件夹删除失败')

    folder.folders.splice(folder.folders.findIndex(f => f.name === call.req.name),1)

    await Global.collection('folders').updateOne({
        course
    },{
        $set:{
            folders:root.folders
        }
    })

    call.succ({
        files:folder.files,
        folders:folder.folders.map(f => f.name)
    })
}